package model 
{
	import flash.display.Sprite;
	import parts.LoaderPart;
	import parts.MainPart;
	import parts.PartManager;
	import util.site.PartCodes;
	
	/**
	 * ...
	 * @author kasoi
	 */
	public class ApplicationView 
	{
		
		//////////////////////////////////////////////////////////
		// Singleton initialization
		//////////////////////////////////////////////////////////
		
		private static var instance: ApplicationView;
		
		private static var allowInstantation: Boolean = true;
		
		public static function getInstance() : ApplicationView
		{
			if (!instance)
			{
				instance = new ApplicationView();
				allowInstantation = false;
			}
			return instance;
		}
	   
		public function ApplicationView() 
		{
			if (!ApplicationView.allowInstantation)
			{
				throw new Error("Error: Instantiation failed.");
			}
		}
		
		
		
		
		//////////////////////////////////////////////////////////
		// Public properties
		//////////////////////////////////////////////////////////
		
		public function get partManager() : PartManager 
		{
			return this._partManager;
		}
		
		public function get content() : Sprite 
		{
			return this._content;
		}
		
		//////////////////////////////////////////////////////////
		// Private properties
		//////////////////////////////////////////////////////////
		
		private var _isInitialized: Boolean = false;
		
		private var _content: Sprite;
		
		private var _partManager: PartManager;
		
		//////////////////////////////////////////////////////////
		// Public methods
		//////////////////////////////////////////////////////////
		
		public function init(container: Sprite) : void 
		{
			if (this._isInitialized) throw new Error('ApplicationView already initialized');
			this._isInitialized = true;
			
			this._content = container;
			
			this.initLayout();
		}
		
		//////////////////////////////////////////////////////////
		// Private methods
		//////////////////////////////////////////////////////////
		
		private function initLayout() : void 
		{
			this.initPartManager();
		}
		
		private function initPartManager() : void 
		{
			this._partManager = new PartManager(PartCodes.LOADER, LoaderPart);
			this.content.addChild(this._partManager);
			// TODO: add parts
			var manager: PartManager = this._partManager;
			manager.addPart(PartCodes.MAIN, MainPart); 
			//manager.addPart
		}
	}
}